/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package View;

import Entity.Appointment;
import Entity.Medical_record;
import Entity.Owner;
import Entity.Pet;
import Entity.Vacine;
import Entity.Veterinarian;
import Services.AppointmentService;
import Services.Medical_recordService;
import Services.OwnerService;
import Services.PetService;
import Services.exceptions.NonexistentEntityException;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.event.TableModelListener;
import javax.swing.filechooser.FileFilter;
import javax.swing.table.TableModel;

/**
 *
 * @author arch
 */
public class DPet extends javax.swing.JDialog {

  /**
   * Creates new form DPet
   */
    private EntityManagerFactory emf=null;
  public DPet(java.awt.Frame parent, boolean modal) {
    super(parent, modal);
    initComponents();
    emf=Persistence.createEntityManagerFactory("HPVASPU");
    fileChooser = new JFileChooser();
    fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
    fileChooser.setAcceptAllFileFilterUsed(false);
    fileChooser.addChoosableFileFilter(new FileFilter() {
      @Override
      public boolean accept(File f) {
        String name = f.getName().toLowerCase();
        return name.endsWith(".jpg") || name.endsWith(".png") || f.isDirectory();
      }
      @Override
      public String getDescription() {
        return "Image Files *.jpg and *.png";
      }
    });
            
    ownerDialog = new JDialog(this, true);
    
    bOwnerDialogOk = new JButton("Ok");
    bOwnerDialogOk.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        
        int index = ownerList.getSelectedIndex();
        selectedOwner = owners.get(index);
        bOwner.setText(selectedOwner.getName());
        ownerDialog.setVisible(false);
      }
    });
    
    bOwnerDialogCancel = new JButton("Cancel");
    bOwnerDialogCancel.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        ownerDialog.setVisible(false);
      }
    });
    
    JScrollPane scrollPane = new JScrollPane();
    ownerList = new JList();
    scrollPane.setViewportView(ownerList);
    
    JPanel pBottom = new JPanel(new FlowLayout(FlowLayout.RIGHT));
    pBottom.add(bOwnerDialogOk);
    pBottom.add(bOwnerDialogCancel);
    ownerDialog.add(scrollPane, BorderLayout.CENTER);
    ownerDialog.add(pBottom, BorderLayout.SOUTH);        
    
    ownerDialog.setTitle("Select Owner");
    ownerDialog.setSize(250, 300);
    ownerDialog.setVisible(false);
    ownerDialog.setLocationRelativeTo(this);
    ownerDialog.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
    
    super.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
    super.pack();
    super.setVisible(false);
    super.setLocationRelativeTo(this);
    
    /*************/
    /* Test Data */
    /*************/
    
    owners = new ArrayList();
    for (int i = 0; i < 10; i++) {
      Owner o = new Owner();
      o.setName(i + "-th Owner");
      o.setLastname(" Wo");
      o.setId((long)i);
      o.setBirthdate(new Date(1990, i, i));
      o.setSSN("000-" + i);
      owners.add(o);
    }
    
    vacines = new ArrayList();
    appointments = new ArrayList();
    
    Veterinarian vet = new Veterinarian();
    vet.setSalary(100);
    vet.setName("Test");
    vet.setLastname("Veterinarian");
    vet.setSpecialities("Test Speciality");
    vet.setSSN("000-01");
    vet.setFocus_of_practice("hahaha");
    vet.setGraduateAt("UNAL");
    vet.setBirthdate(Calendar.getInstance().getTime());
    
    for (int i = 0; i < 10; i++) {
      Vacine v = new Vacine();
      v.setDosage(12.5f);
      v.setName("Vacinne No. " + i);
      vacines.add(v);
      
      Appointment a = new Appointment();
      a.setCharge(10 + i);
      a.setDate(Calendar.getInstance().getTime());
      a.setNotes("blablabla " + i);
      a.setVet(vet);
      appointments.add(a);
    }
  }

  /**
   * This method is called from within the constructor to initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is always
   * regenerated by the Form Editor.
   */
  @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {

    lWeight = new javax.swing.JLabel();
    bClose = new javax.swing.JButton();
    tfName = new javax.swing.JTextField();
    bSave = new javax.swing.JButton();
    bOwner = new javax.swing.JButton();
    bPhoto = new javax.swing.JButton();
    bDelete = new javax.swing.JButton();
    lOwner = new javax.swing.JLabel();
    tfWeight = new javax.swing.JTextField();
    lSpecie = new javax.swing.JLabel();
    bNewVacine = new javax.swing.JButton();
    bNewAppointment = new javax.swing.JButton();
    jScrollPane3 = new javax.swing.JScrollPane();
    pMedicalRecord = new javax.swing.JPanel();
    lMedicalRecord = new javax.swing.JLabel();
    lVaccine = new javax.swing.JLabel();
    jScrollPane1 = new javax.swing.JScrollPane();
    tableVacines = new javax.swing.JTable();
    jScrollPane2 = new javax.swing.JScrollPane();
    tableAppointments = new javax.swing.JTable();
    lAppointment = new javax.swing.JLabel();
    lAge = new javax.swing.JLabel();
    tfSpecie = new javax.swing.JTextField();
    lName = new javax.swing.JLabel();
    tfAge = new javax.swing.JTextField();

    setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

    lWeight.setText("Weight:");

    bClose.setText("Close");
    bClose.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        bCloseActionPerformed(evt);
      }
    });

    bSave.setText("Save");
    bSave.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        bSaveActionPerformed(evt);
      }
    });

    bOwner.setText("Unknown");
    bOwner.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        bOwnerActionPerformed(evt);
      }
    });

    bPhoto.setText("Photo");
    bPhoto.setContentAreaFilled(false);
    bPhoto.setFocusPainted(false);
    bPhoto.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        bPhotoActionPerformed(evt);
      }
    });

    bDelete.setText("Delete");
    bDelete.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        bDeleteActionPerformed(evt);
      }
    });

    lOwner.setText("Owner:");

    lSpecie.setText("Specie:");

    bNewVacine.setText("New Vacine");
    bNewVacine.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        bNewVacineActionPerformed(evt);
      }
    });

    bNewAppointment.setText("New Appointment");
    bNewAppointment.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        bNewAppointmentActionPerformed(evt);
      }
    });

    lMedicalRecord.setFont(new java.awt.Font("Dialog", 1, 14)); // NOI18N
    lMedicalRecord.setText("Medical Record:");

    lVaccine.setText("Vaccines:");

    tableVacines.setModel(new javax.swing.table.DefaultTableModel(
      new Object [][] {

      },
      new String [] {
        "Name", "Dossage"
      }
    ) {
      Class[] types = new Class [] {
        java.lang.String.class, java.lang.Float.class
      };

      public Class getColumnClass(int columnIndex) {
        return types [columnIndex];
      }
    });
    jScrollPane1.setViewportView(tableVacines);

    tableAppointments.setModel(new javax.swing.table.DefaultTableModel(
      new Object [][] {

      },
      new String [] {
        "Date", "Charge", "Notes", "Veterinarian"
      }
    ) {
      Class[] types = new Class [] {
        java.lang.String.class, java.lang.Float.class, java.lang.String.class, java.lang.String.class
      };

      public Class getColumnClass(int columnIndex) {
        return types [columnIndex];
      }
    });
    jScrollPane2.setViewportView(tableAppointments);

    lAppointment.setText("Appointments:");

    javax.swing.GroupLayout pMedicalRecordLayout = new javax.swing.GroupLayout(pMedicalRecord);
    pMedicalRecord.setLayout(pMedicalRecordLayout);
    pMedicalRecordLayout.setHorizontalGroup(
      pMedicalRecordLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 816, Short.MAX_VALUE)
      .addComponent(jScrollPane2)
      .addGroup(pMedicalRecordLayout.createSequentialGroup()
        .addContainerGap()
        .addGroup(pMedicalRecordLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addComponent(lMedicalRecord)
          .addComponent(lVaccine)
          .addComponent(lAppointment))
        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );
    pMedicalRecordLayout.setVerticalGroup(
      pMedicalRecordLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(pMedicalRecordLayout.createSequentialGroup()
        .addContainerGap()
        .addComponent(lMedicalRecord)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(lVaccine)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(lAppointment)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addContainerGap())
    );

    jScrollPane3.setViewportView(pMedicalRecord);

    lAge.setText("Age:");

    lName.setText("Name:");

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addContainerGap()
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addGroup(layout.createSequentialGroup()
            .addComponent(bPhoto, javax.swing.GroupLayout.PREFERRED_SIZE, 314, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
              .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                  .addComponent(lSpecie)
                  .addComponent(lAge)
                  .addComponent(lWeight)
                  .addComponent(lName))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                  .addComponent(tfName)
                  .addComponent(tfSpecie)
                  .addComponent(tfAge)
                  .addComponent(tfWeight)))
              .addGroup(layout.createSequentialGroup()
                .addComponent(lOwner)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(bOwner, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
          .addComponent(jScrollPane3, javax.swing.GroupLayout.Alignment.TRAILING)
          .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addGap(0, 0, Short.MAX_VALUE)
            .addComponent(bNewAppointment)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(bNewVacine)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(bDelete)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(bSave)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(bClose)))
        .addContainerGap())
    );
    layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(bPhoto, javax.swing.GroupLayout.PREFERRED_SIZE, 215, javax.swing.GroupLayout.PREFERRED_SIZE))
          .addGroup(layout.createSequentialGroup()
            .addGap(36, 36, 36)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
              .addComponent(lName)
              .addComponent(tfName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGap(18, 18, 18)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
              .addComponent(lSpecie)
              .addComponent(tfSpecie, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGap(18, 18, 18)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
              .addComponent(lAge)
              .addComponent(tfAge, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGap(18, 18, 18)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
              .addComponent(lWeight)
              .addComponent(tfWeight, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
              .addComponent(lOwner)
              .addComponent(bOwner))))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
        .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 346, Short.MAX_VALUE)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(bClose)
          .addComponent(bSave)
          .addComponent(bDelete)
          .addComponent(bNewVacine)
          .addComponent(bNewAppointment))
        .addContainerGap())
    );

    pack();
  }// </editor-fold>//GEN-END:initComponents

  private void bCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bCloseActionPerformed
  
    setVisible(false);
  }//GEN-LAST:event_bCloseActionPerformed

  private void bOwnerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bOwnerActionPerformed
      
      // llamar al DAO aqui' para obtener la lista de owners
    owners = null;
    OwnerService os=new OwnerService(emf);
    owners=os.findOwnerEntities();
    
    DefaultListModel listModel = new DefaultListModel();
    
    for (int i = 0; i < owners.size(); i++) {
      Owner o = owners.get(i);
      listModel.addElement(o.getSSN() + " | " + o.getName());
    }
    ownerList.setModel(listModel);
    ownerDialog.setVisible(true);
  }//GEN-LAST:event_bOwnerActionPerformed

  private void bDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bDeleteActionPerformed
    
    if (!saved) {
      JOptionPane.showMessageDialog(this, "Pet not saved yet", "Info", JOptionPane.INFORMATION_MESSAGE);
      return;
    }
    int op = DeleteCancel("Are You Sure?", "Delete");
    if (op == JOptionPane.NO_OPTION) return;
    
      // llamar al DAO aqui'
    PetService ps=new PetService(emf);
        try {
            ps.destroy(pet.getId());
        } catch (NonexistentEntityException ex) {
            Logger.getLogger(DPet.class.getName()).log(Level.SEVERE, null, ex);
            JOptionPane.showMessageDialog(this,"The id of the pet was not found");
        }
    setVisible(false);
  }//GEN-LAST:event_bDeleteActionPerformed

  private void bSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bSaveActionPerformed
    
    int age = 0;
    float weight = 0;
    
    boolean ok = true;
    // Chekear valores en los campos aqui'

    // asigne el resultado a ok
    
    if (!ok) {
      JOptionPane.showMessageDialog(this, "Invalid data", "Error", JOptionPane.ERROR_MESSAGE);
      return;
    }
    saved = true;
    age = Integer.parseInt(tfAge.getText());
    weight = Float.parseFloat(tfWeight.getText());
    
    Medical_record medicalRecord = new Medical_record();
    medicalRecord.setAppointments(appointments);
    medicalRecord.setVacines(vacines);
    medicalRecord.setPet(pet);
//    pet.setMedicalRecord(medicalRecord);
    
    pet.setAge(age);
    pet.setName(tfName.getText());
    pet.setSpecies(tfSpecie.getText());
    pet.setWeight(weight);
    pet.setOwner(selectedOwner);
    pet.setPhotoPath(photoPath);
//    selectedOwner.getPets().add(pet);
    
    // llamar al DAO aqui'
    // tanto para pet, selectedOwner, medicalRecord
//    OwnerService os=new OwnerService(emf);
//    Medical_recordService mrs=new Medical_recordService(emf);
    PetService ps=new PetService(emf);
    ps.create(pet);
    clear();
//        try {
//            mrs.edit(medicalRecord);
//        } catch (NonexistentEntityException ex) {
//            Logger.getLogger(DPet.class.getName()).log(Level.SEVERE, null, ex);
//        } catch (Exception ex) {
//            Logger.getLogger(DPet.class.getName()).log(Level.SEVERE, null, ex);
//        }
//        try {
//            os.edit(selectedOwner);
//        } catch (NonexistentEntityException ex) {
//            Logger.getLogger(DPet.class.getName()).log(Level.SEVERE, null, ex);
//        } catch (Exception ex) {
//            Logger.getLogger(DPet.class.getName()).log(Level.SEVERE, null, ex);
//        }
  }//GEN-LAST:event_bSaveActionPerformed

  private void bPhotoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bPhotoActionPerformed
    
    int res = fileChooser.showOpenDialog(this);
    if (res == JFileChooser.APPROVE_OPTION) {
      photoPath = fileChooser.getSelectedFile().getAbsolutePath();
      bPhoto.setText("");
      bPhoto.setIcon(new ImageIcon(photoPath));
    }
  }//GEN-LAST:event_bPhotoActionPerformed

  private void bNewVacineActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bNewVacineActionPerformed
    
    Vacine vac = null;
      // Crear nueva vacuna aqui'
    vacines.add(vac);
    tableVacines.setModel(new VacineTableModel());
  }//GEN-LAST:event_bNewVacineActionPerformed

  private void bNewAppointmentActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bNewAppointmentActionPerformed
    
    Appointment app = null;
      // Crear Apunte aqui'
    AppointmentService as=new AppointmentService(emf);
 
    appointments.add(app);
    tableAppointments.setModel(new AppointmentTableModel());
  }//GEN-LAST:event_bNewAppointmentActionPerformed

  public void set(Pet p) {
    
    if (p == null) {
      saved = false;
      clear();
      pet = new Pet();
      setTitle("New Pet");
    } else {
      pet = p;
      
      saved = true;
      setTitle(p.getName());
      tfName.setText(p.getName());
      tfSpecie.setText(p.getSpecies());
      tfAge.setText(p.getAge() + "");
      tfWeight.setText(p.getWeight() + "");
      bPhoto.setText("");
      if (pet.getPhotoPath().length() == 0) {
        bPhoto.setText("Select Photo");
      }
      bPhoto.setIcon(new ImageIcon(pet.getPhotoPath()));
      if (p.getOwner() != null) {
        bOwner.setText(p.getOwner().getName());
      } else {
        bOwner.setText("Unknown");
      }
      // ---------------------------------------------------
      // Decomentar estas lineas cuando no se use el ejemplo
      // ---------------------------------------------------
//       appointments = p.getMedicalRecord().getAppointments();
//       vacines = p.getMedicalRecord().getVacines();
//      tableAppointments.setModel(new AppointmentTableModel());
//      tableVacines.setModel(new VacineTableModel());
    }
  }
  
  public Pet get() {
    return pet;
  }
  
  public void clear() {
    
    saved = false;
    selectedOwner = null;
    tfName.setText("");
    tfSpecie.setText("");
    tfAge.setText("");
    tfWeight.setText("");
    bOwner.setText("Unknown");
    bPhoto.setIcon(null);
    bPhoto.setText("Select Photo");
    
    vacines.clear();
    appointments.clear();
    tableAppointments.setModel(new AppointmentTableModel());
    tableVacines.setModel(new VacineTableModel());
  }
  
  public int DeleteCancel(String message, String title) {
    
    Object[] options = {"Yes", "Cancel"};
    int op = JOptionPane.showOptionDialog(this, message, title,
                                          JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE,
                                          null, options, JOptionPane.YES_OPTION);
    return op;
  }
  
  Pet pet;
  Owner selectedOwner;
  List<Vacine> vacines;
  List<Appointment> appointments;
          
  JList ownerList;
  JButton bOwnerDialogCancel;
  JButton bOwnerDialogOk;
  JDialog ownerDialog;
  String photoPath;
  
  List<Owner> owners;
  
  boolean saved;
  JFileChooser fileChooser;
  
  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JButton bClose;
  private javax.swing.JButton bDelete;
  private javax.swing.JButton bNewAppointment;
  private javax.swing.JButton bNewVacine;
  private javax.swing.JButton bOwner;
  private javax.swing.JButton bPhoto;
  private javax.swing.JButton bSave;
  private javax.swing.JScrollPane jScrollPane1;
  private javax.swing.JScrollPane jScrollPane2;
  private javax.swing.JScrollPane jScrollPane3;
  private javax.swing.JLabel lAge;
  private javax.swing.JLabel lAppointment;
  private javax.swing.JLabel lMedicalRecord;
  private javax.swing.JLabel lName;
  private javax.swing.JLabel lOwner;
  private javax.swing.JLabel lSpecie;
  private javax.swing.JLabel lVaccine;
  private javax.swing.JLabel lWeight;
  private javax.swing.JPanel pMedicalRecord;
  private javax.swing.JTable tableAppointments;
  private javax.swing.JTable tableVacines;
  private javax.swing.JTextField tfAge;
  private javax.swing.JTextField tfName;
  private javax.swing.JTextField tfSpecie;
  private javax.swing.JTextField tfWeight;
  // End of variables declaration//GEN-END:variables

  /* ----------------------------------- */
  /* TableModel for the Appointment Data Table */
  /* ----------------------------------- */
  
  class AppointmentTableModel implements TableModel {
    public AppointmentTableModel() {}

    public int getRowCount() {
      return appointments.size(); 
    }
    public int getColumnCount() {
      return 4;
    }
    public String getColumnName(int c) {
      
      if (c == 0) return "Date";
      if (c == 1) return "Charge";
      if (c == 2) return "Notes";
      if (c == 3) return "Veterinarian";
      return null;
    }
    public Class<?> getColumnClass(int c) {
      if (c == 0) return Date.class;
      if (c == 1) return String.class;
      if (c == 2) return String.class;
      if (c == 3) return String.class;
      return null;
    }
    public boolean isCellEditable(int r, int c) {
      return false;
    }
    public Object getValueAt(int r, int c) {
      if (c == 0) {
        return appointments.get(r).getDate();
      }
      else if (c == 1) {
        return appointments.get(r).getCharge() + "";
      }
      else if (c == 2) {
        return appointments.get(r).getNotes();
      }
      else if (c == 3) {
        return appointments.get(r).getVet().getName();
      }
      return null;
    }
    public void setValueAt(Object o, int r, int c) {}
    public void addTableModelListener(TableModelListener tl)    {}
    public void removeTableModelListener(TableModelListener tl) {}
  }
  
  /* ----------------------------------- */
  /* TableModel for the Vacine Data Table */
  /* ----------------------------------- */
  
  class VacineTableModel implements TableModel {
    public VacineTableModel() {}

    public int getRowCount() {
      return vacines.size(); // Name Dossage
    }
    public int getColumnCount() {
      return 2;
    }
    public String getColumnName(int c) {
      
      if (c == 0) return "Name";
      if (c == 1) return "Dossage";
      return null;
    }
    public Class<?> getColumnClass(int c) {
      if (c == 0) return String.class;
      if (c == 1) return String.class;
      return null;
    }
    public boolean isCellEditable(int r, int c) {
      return false;
    }
    public Object getValueAt(int r, int c) {
      if (c == 0) {
        return vacines.get(r).getName();
      }
      else if (c == 1) {
        return vacines.get(r).getDosage() + "";
      }
      return null;
    }
    public void setValueAt(Object o, int r, int c) {}
    public void addTableModelListener(TableModelListener tl)    {}
    public void removeTableModelListener(TableModelListener tl) {}
  }
}
